@extends('develop.layoutEdit')

@section('css')
    <link rel="stylesheet" type="text/css" href="/static/libs/uploadify/uploadify.css?v=20170509">
    <style>
        .uploadify {
            float: left;
            margin-right: 10px;
        }
        .del_img{
            float:left;
            padding-top:30px;
            margin:10px;
        }
        .pic{
            margin-right:60px;
            float:left;
            padding-top:5px;
        }
    </style>
@endsection

@section('title')
    @if(isset($id) && $id>0)
        <li class="cur">
            <span>修改模块 </span>
        </li>
    @else
        <li class="cur">
            <span>添加模块 </span>
        </li>
    @endif
@endsection

@section('content')

    <div class="form-group">
        <label class="col-sm-3 control-label"><span class="red mr5">*&nbsp;</span>模块名称：</label>
        <div class="col-sm-3">
            <input type="text" class="form-control"  id="module_name" name="module_name"  placeholder="请输入模块名称" value="{{ $module['module_name'] or ''}}">
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-3 control-label"><span class="red pr5">*&nbsp;</span>模块类型：</label>
        <div class="col-sm-3">
            <select name="module_type" id="module_type" class="form-control" placeholder="模块类型">
                <option value="0">请选择</option>
                <option value="1">商品</option>
                <option value="2">图片</option>
            </select>
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-3 control-label"><span class="red pr5">*&nbsp;</span>展示图片：</label>
        <div class="col-sm-5">
            <input type="file"  id="upload_img_0" style="float: left;"/>
            <div id="pic-box" class="pic">
                <img style="float: left;width: 200px;height: 100px;"  class="img-rounded show_img" src="{{$module['show_pic'] or ''}}">
                <a href="javascript: void(0);" id="del_log_img" class="del_img">删除</a>
            </div>
            <input type="hidden" class="form-control" id="show_pic" name ="show_pic" value="{{$module['show_pic'] or ''}}"/>
        </div>
    </div>

    <div class="form-group ">
        <label class="col-sm-3 control-label"><span class="red pr5">*&nbsp;</span>HTML代码：</label>
        <div class="col-sm-5">
            <textarea name="html" class="form-control" id="html" rows="10"  placeholder="请输入HTML代码">{{ $module['html'] or ''}}</textarea>
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-3 control-label"><span class="red mr5">*&nbsp;</span>默认样式库名称：</label>
        <div class="col-sm-3">
            <input type="text" class="form-control"  id="style_name" name="style_name"  placeholder="请输入默认样式库名称" value="{{ $style[0]['style_name'] or ''}}">
        </div>
    </div>

    <div class="form-group ">
        <label class="col-sm-3 control-label"><span class="red pr5">*&nbsp;</span>默认样式库CSS：</label>
        <div class="col-sm-5">
            <textarea name="style_css" class="form-control" id="style_css" rows="10"  placeholder="请输入样式库css代码">{{ $style[0]['style_css'] or ''}}</textarea>
        </div>
    </div>

    <div class="form-group" id="edit">
        <label class="col-sm-3 control-label"></label>
        <div class="col-sm-9">
            <input type="hidden" name="id" id="id" value="{{ $id or ''}}">
            <input type="button"  value="保存" onclick="Module.edit();" class="btn btn-success" >
        </div>
    </div>

@endsection

@section('js')
    <script type="text/javascript" src="/static/libs/uploadify/jquery.uploadify.min.js"></script>
    <script type="text/javascript" src="/static/ebsig/jquery.photo.min.js?v=20151219"></script>
    <script>

        //删除图片
        $(document).on('click', '#pic-box a', function () {
            Module.removePhoto();
        });

        //修改时下拉框赋值
        $(function(){

            var module_type = {!! $module['module_type'] or 0 !!};

            if ( module_type ) {

                $('#module_type').find("option[value="+module_type+"]").attr('selected',true);

            }
        });

        var Module = {

            //保存
            edit:function () {

                var data = E.getFormValues('edit-form');
                var message ='';
                if (E.isEmpty(data.module_name) ) {
                    message += '请输入模块名称</br>';
                }
                if (E.isEmpty(data.module_type) || data.module_type ==0) {
                    message += '请选择模块类型</br>';
                }
                if (E.isEmpty(data.show_pic) ) {
                    message += '请选择展示图片</br>';
                }
                if (E.isEmpty(data.html) ) {
                    message += '请输入HTML代码</br>';
                }
                if (E.isEmpty(data.style_name) ) {
                    message += '请输入默认样式库名称</br>';
                }
                if (E.isEmpty(data.style_css) ) {
                    message += '请输入默认样式库CSS</br>';
                }

                if ( message ) {
                    layer.alert(message,{icon:2});
                    return false;
                }

                if ( data.id && data.id>0 ) {
                    var t = '您确认要修改该模块信息吗';
                } else {
                    var t = '您确认要添加该模块信息吗';
                }

                layer.confirm(t,{icon:3},function(index){

                    layer.close(index);
                    E.ajax({
                        type:'get',
                        url: '/lar/develop/module/store',
                        data: data,
                        dataType:'json',
                        // 返回值判断
                        success: function (obj) {
                            if ( obj.code == 200 ) {
                                layer.alert( obj.message , { icon: 1,time : 1500 } ) ;
                                parent.Module.load();
                                setTimeout('E.layerClose();' ,1000 );
                            } else {
                                layer.alert(obj.message, {icon: 2});
                            }

                        }
                    });
                });

            },
            removePhoto: function () {
                $('.show_img').hide();
                $('#del_log_img').hide();
                $('#show_pic').val('');
            }
        };

        $(window).ready(function() {

            //上传控件
            $("input[id*= 'upload_img_']").uploadify({
                buttonText    :  '<div>上传图片</div>',
                height        : 35,
                swf           : '/static/libs/uploadify/uploadify.swf',
                uploader      : '/public/filetransfer/upload?action=customer/rank',
                width         : 120,
                onUploadSuccess: function(file, res) {
                    res = eval('(' + res + ')');
                    if (res.code == 200) {

                        var url = res.data.path;
                        var id = (this.button[0].attributes[0].nodeValue.split("_")[2]).split("-")[0];
                        if (id == 0 ) {
                            $('#pic-box a').show();
                        }
                        $('.show_img').attr('src',url);
                        $('.show_img').show();
                        $('#show_pic' ).val(url) ;
                    } else {
                        layer.alert(res.message);
                    }
                }
            });

        });

    </script>
@endsection